using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Users {
	class UserAppearanceProfileDA {

		private const string SP_GetAll = "dbo.UserAppearanceProfile_GetAll";
		private const string SP_GetByUserKey = "dbo.UserAppearanceProfile_GetByUserKey";
		private const string SP_Insert = "dbo.UserAppearanceProfile_Insert";
		private const string SP_Update = "dbo.UserAppearanceProfile_Update";

		public static UserApperanceProfileEnt CreateFromReader(IDataReader reader) {
			UserApperanceProfileEnt item = new UserApperanceProfileEnt();
			item.UserKey = ConvertUtils.ToString(reader["UserKey"]);
			item.Height = ConvertUtils.ToShort(reader["Height"]);
			item.Weight = ConvertUtils.ToShort(reader["Weight"]);
			item.HairStyleID = ConvertUtils.ToShort(reader["HairStyleID"]);
			item.HairStyle = ConvertUtils.ToString(reader["HairStyle"]);
			item.BodyID = ConvertUtils.ToShort(reader["BodyID"]);
			item.Body = ConvertUtils.ToString(reader["Body"]);
			item.AttractionID = ConvertUtils.ToShort(reader["AttractionID"]);
			item.Attraction = ConvertUtils.ToString(reader["Attraction"]);
			item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
			item.DateModified = ConvertUtils.ToDateTime(reader["DateModified"]);
			return item;
		}

        public static List<UserApperanceProfileEnt> GetAll(string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			List<UserApperanceProfileEnt> result = new List<UserApperanceProfileEnt>();
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetAll)) {
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					while (reader.Read()) {
						result.Add(CreateFromReader(reader));
					}
				}
			}
			return result;
		}

		public static UserApperanceProfileEnt GetByUserKey(string userKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			UserApperanceProfileEnt result = null;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetByUserKey)) {
				db.AddParameter(cmd, "UserKey", userKey);
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					if (reader.Read()) {
						result = CreateFromReader(reader);
					}
				}
			}
			return result;
		}

        public static int Insert(UserApperanceProfileEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
				db.AddParameter(cmd, "UserKey", item.UserKey);
				db.AddParameter(cmd, "Height", item.Height);
				db.AddParameter(cmd, "Weight", item.Weight);
				db.AddParameter(cmd, "HairStyleID", item.HairStyleID);
				db.AddParameter(cmd, "BodyID", item.BodyID);
				db.AddParameter(cmd, "AttractionID", item.AttractionID);
				db.AddParameter(cmd, "DateCreated", item.DateCreated);
				db.AddParameter(cmd, "DateModified", item.DateModified);

				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

        public static int Update(UserApperanceProfileEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Update)) {
				db.AddParameter(cmd, "UserKey", item.UserKey);
				db.AddParameter(cmd, "Height", item.Height);
				db.AddParameter(cmd, "Weight", item.Weight);
				db.AddParameter(cmd, "HairStyleID", item.HairStyleID);
				db.AddParameter(cmd, "BodyID", item.BodyID);
				db.AddParameter(cmd, "AttractionID", item.AttractionID);
				db.AddParameter(cmd, "DateModified", item.DateModified);

				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

	}
}